/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.tedu.mall.ams.controller;

import cn.tedu.mall.ams.service.IAdminService;
import cn.tedu.mall.common.pojo.admin.dto.AdminAddDTO;
import cn.tedu.mall.common.pojo.admin.dto.AdminQueryDTO;
import cn.tedu.mall.common.pojo.admin.dto.AdminUpdateDTO;
import cn.tedu.mall.common.pojo.admin.vo.AdminVO;
import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/ams/admin")
@Api(tags = "后台用户模块")
@Slf4j
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "新增后台账号")
    @GetMapping("/save")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @CacheEvict(cacheNames = "admin", allEntries = true)
    public JsonResult addAdmin(AdminAddDTO adminDTO) {
        adminService.addAdmin(adminDTO);
        return JsonResult.ok();
    }

    /**
     * 查询admin列表
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "查询后台用户列表")
    @GetMapping("")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "页数", name = "pageNum"),
        @ApiImplicitParam(value = "条数", name = "sizeNum")})
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<JsonPage<AdminVO>> listAdmins(Integer pageNum, Integer sizeNum) {
        JsonPage<AdminVO> jsonPage = adminService.listAdmins(pageNum, sizeNum);
        return JsonResult.ok(jsonPage);
    }

    //利用用户名搜索
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "利用账号搜索")
    @GetMapping("/query")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "页数", name = "pageNum", required = true),
        @ApiImplicitParam(value = "条数", name = "sizeNum", required = true),
        @ApiImplicitParam(value = "查询条件", name = "query", required = true)})
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<JsonPage<AdminVO>> queryAdmins(Integer pageNum, Integer sizeNum, String query) {
        JsonPage<AdminVO> jsonPage = adminService.queryAdmins(pageNum, sizeNum, query);
        return JsonResult.ok(jsonPage);
    }

    /**
     * 编辑账号
     */
    @ApiOperationSupport(order = 40)
    @ApiOperation(value = "编辑后台用户")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @CacheEvict(cacheNames = "admin", allEntries = true)
    public JsonResult updateAdmin(AdminUpdateDTO adminUpdateDTO) {
        adminService.updateAdmin(adminUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 50)
    @ApiOperation(value = "删除后台用户")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @CacheEvict(cacheNames = "admin", allEntries = true)
    public JsonResult deleteAdmin(Long id) {
        adminService.deleteAdmin(id);
        return JsonResult.ok();
    }

    /**
     * 停用启用用户
     */
    @ApiOperationSupport(order = 60)
    @ApiOperation(value = "停用启用用户")
    @PostMapping("/enable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @ApiImplicitParams({
        @ApiImplicitParam(value = "管理员id", name = "id", required = true),
        @ApiImplicitParam(value = "启用值 0 不起用 1 启用", name = "enable", required = true)
    })
    @CacheEvict(cacheNames = "admin", allEntries = true)
    public JsonResult enableAdmin(Long id, Integer enable) {
        adminService.enableAdmin(id, enable);
        return JsonResult.ok();
    }

    @ApiOperationSupport(order = 70)
    @ApiOperation(value = "按条件查询后台用户列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @Cacheable(cacheNames = "admin", key = "#adminQueryDTO.toString()")
    public JsonResult<JsonPage<AdminVO>> queryListAdmin(AdminQueryDTO adminQueryDTO) {
        JsonPage<AdminVO> admins = adminService.queryListAdmin(adminQueryDTO);
        return JsonResult.ok(admins);
    }

}
